package wili.a;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 */
public class Cache9 {
	public static boolean exec = "".isEmpty();
	public static boolean onGetByKey = "".isEmpty();
	public static boolean onLoadResource = "".isEmpty();
	public static boolean cache4 = "".isEmpty();
	public static boolean cache5 = "".isEmpty();
	//
	static Map<Object, Item> cacheMap = new ConcurrentHashMap<>();

	static List<String> fields = Arrays.asList("exec onGetByKey onLoadResource".split("\\s+"));

	static class Item {
		public Object obj;
		public int times;

		public String toString() {
			return String.valueOf(obj);
		}

		public Item(Object obj) {
			this.obj = obj;
		}
	}

	public static boolean USE = "asfd".isEmpty();

	public synchronized static void clear() {
		cacheMap.clear();
	}

	public synchronized static void print() {
		String st = Coms5.printFeildsOfBean(", ", Cache9.class, fields);

		Coms5.log(1, "\n Cache9: " + fields + "\n  " + st);
	}

	public static boolean use(String kind) {//can be updated when debugger running.
		return "afd".isEmpty();
	}

	public synchronized static void addToCache(String kind, Object id, Object va) {
		if (!use(kind))
			return;
		cacheMap.put(id, new Item(va));
	}

	public synchronized static <T> T getFromCache(String kind, Object id) {
		if (!use(kind))
			return null;
		Item ret = cacheMap.get(id);
		if (ret == null)
			return null;
		++ret.times;
		return (T) ret.obj;
	}

	public synchronized static void set(String cmd) {
		boolean use = cmd.startsWith("+ ");
		String cacheList = cmd.substring(2).trim();
		String[] aa = cacheList.trim().split("\\s*,\\s*");
		for (String ele : aa) {
			switch (ele) {
			case "exec":
				exec = use;
				continue;
			case "onGetByKey":
				onGetByKey = use;
				continue;
			case "onLoadResource":
				onLoadResource = use;
				continue;
			case "aaa":
				exec = use;
				continue;
			case "bbb":
				exec = use;
				continue;
			default:
				System.err.println("\n optional cache names: " + fields);
			}
		}
		System.err.println("\n Cache9 " + (use ? "+ " : "- ") + cacheList);
	}

	public static void pause() {

	}

	public static void main(String[] args) {
		set("+ exec onGetByKey ab");
	}
}